//
//  BCShopViewController.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/13.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCShopViewController.h"
#import "ShopGroup.h"
#import "BCStuffChooseViewController.h"
#import "LJHouseEnterCell.h"
#import "LJBannerCell.h"
#import "LJKnowledgeTitleCell.h"
#import "LJKnowledgeInfoCell.h"
#import "BCAlwaysBuyCollectionViewCell.h"

// 快速创建颜色
#define LJColor(r, g, b, a) [UIColor colorWithRed:r green:g blue:b alpha:a]

@interface BCShopViewController () <UICollectionViewDataSource,UICollectionViewDelegate,LJBannerCellDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
/** 搜索框绿色渐变背景视图 */
@property (nonatomic, strong) UIView *greenView;
/** 搜索view */
@property (nonatomic, strong) UIView *searchView;
/** 组的集合 */
@property (nonatomic, strong) NSMutableArray *groups;
/** 特惠与新品切换标示 */
@property (nonatomic, assign) NSInteger segFlag;

@end

@implementation BCShopViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = YES;
    
    [self p_initMain];
    [self p_initTopView];
    [self p_initSearchView];
    
    //初始化数据
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private
/**
 *  初始化主视图view
 */
- (void)p_initMain {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:244.f/255.f green:244.f/255.f blue:244.f/255.f alpha:1];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    //设置contentInset属性（上左下右的值）
    self.collectionView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
    //注册collectionView组头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"kHeaderID"];
    self.segFlag = 1;
}

/**
 *  初始化顶部视图view
 */
- (void)p_initTopView {
    self.greenView.frame = CGRectMake(0, 0, self.view.frame.size.width , 64);
    self.greenView.backgroundColor = LJColor(0.19, 0.63, 0.34, 1);
    self.greenView.alpha = 0.95f;
    [self.view addSubview:self.greenView];
}

/**
 *  初始化搜索框view
 */
- (void)p_initSearchView {
    UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(23.f, 17.f, 100.f, 44.f)];
    cityLabel.text = @"大连";
    cityLabel.font = [UIFont boldSystemFontOfSize:20];
    cityLabel.textColor = [UIColor whiteColor];
    [self.greenView addSubview:cityLabel];
    
    UIView *searchBar = [[UIView alloc] initWithFrame:CGRectMake(88.f, 22.f, self.greenView.frame.size.width-88.f-16.f, 35.f)];
    searchBar.backgroundColor = [UIColor whiteColor];
    searchBar.layer.cornerRadius = 8.f;
    [self.greenView addSubview:searchBar];
    
    UIImageView *searchIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Search"]];
    searchIconView.frame = CGRectMake(8.f, 7.f, 21.f, 21.f);
    [searchBar addSubview:searchIconView];
    
    UILabel *tipLable = [[UILabel alloc] initWithFrame:CGRectMake(30.f, 0, 100.f, searchBar.frame.size.height)];
    tipLable.text = @"大连邦采";
    tipLable.backgroundColor = [UIColor clearColor];
    tipLable.font = [UIFont systemFontOfSize:17.f];
    tipLable.textAlignment = NSTextAlignmentCenter;
    tipLable.textColor = [UIColor colorWithRed:220.f/255.f green:220.f/255.f blue:220.f/255.f alpha:1];
    [searchBar addSubview:tipLable];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(88.f, 22.f, 300.f, 35.f);
    searchButton.backgroundColor = [UIColor clearColor];
    searchButton.layer.cornerRadius = 8.f;
    [searchButton addTarget:self action:@selector(p_startSearch) forControlEvents:UIControlEventTouchUpInside];
    [self.greenView addSubview:searchButton];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.groups.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[self.groups[section] items] count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        static NSString *ID = @"BannerCell";
        LJBannerCell *cell = [LJBannerCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
    else if (indexPath.section == 1) {
        static NSString *ID = @"houseCell";
        LJHouseEnterCell *cell = [LJHouseEnterCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.iconImageView.image = [UIImage imageNamed:@"Group"];
            cell.titleLable.text = @"禽蛋肉品";
        }
        else if (indexPath.row == 1) {
            cell.iconImageView.image = [UIImage imageNamed:@"Group"];
            cell.titleLable.text = @"新鲜蔬菜";
        }
        else if (indexPath.row == 2) {
            cell.iconImageView.image = [UIImage imageNamed:@"Group"];
            cell.titleLable.text = @"水产冻品";
        }
        else if (indexPath.row == 3) {
            cell.iconImageView.image = [UIImage imageNamed:@"Group"];
            cell.titleLable.text = @"水果干果";
        }
        else if (indexPath.row == 4) {
            cell.iconImageView.image = [UIImage imageNamed:@"Group"];
            cell.titleLable.text = @"酒水饮料";
        }
        else if (indexPath.row == 5) {
            cell.iconImageView.image = [UIImage imageNamed:@"Group"];
            cell.titleLable.text = @"调料熟食";
        }
        else if (indexPath.row == 6) {
            cell.iconImageView.image = [UIImage imageNamed:@"Group"];
            cell.titleLable.text = @"米面粮油";
        }
        else {
            cell.iconImageView.image = [UIImage imageNamed:@"Group"];
            cell.titleLable.text = @"厨房用品";
        }
        
        return cell;
    }
    else if (indexPath.section == 2) {
        static NSString *ID = @"KnowledgeTitleCell";
        LJKnowledgeTitleCell *cell = [LJKnowledgeTitleCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        __typeof(self) weakSelf = self;
        cell.segBlock = ^(int flag){
            weakSelf.segFlag = flag;
            [collectionView reloadData];
        };
        return cell;
    }
    else {
        if (self.segFlag == 1) {
            static NSString *ID = @"AlwaysBuyCell";
            BCAlwaysBuyCollectionViewCell *cell = [BCAlwaysBuyCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
        else {
            static NSString *ID = @"KnowledgeInfoCell";
            LJKnowledgeInfoCell *cell = [LJKnowledgeInfoCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    
    if (section == 0) {
        UIEdgeInsets top = {0,25,0,25};
        return top;
    }
    else if (section == 1) {
        UIEdgeInsets top = {20,25,15,25};
        return top;
    }
    else if (section == 2) {
        UIEdgeInsets top = {0,25,0,25};
        return top;
    }
    else {
        UIEdgeInsets top = {0,25,55,25};
        return top;
    }
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(self.view.frame.size.width, 210);
    }
    else if (indexPath.section == 1) {
        return CGSizeMake((self.view.frame.size.width-(25.f*5))/4, 92);
    }
    else if (indexPath.section == 2) {
        return CGSizeMake(self.view.frame.size.width, 56);
    }
    else {
        if (_segFlag == 1) {
            return CGSizeMake(self.view.frame.size.width, 80);
        }
        else {
            return CGSizeMake(self.view.frame.size.width, 114);
        }
    }
    
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 25;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section == 0) {
        return 10;
    }
    else if (section == 1) {
        return 10;
    }
    else if (section == 2) {
        return 10;
    }
    else {
        return 5;
    }
    
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        BCStuffChooseViewController *chooseController = [[BCStuffChooseViewController alloc] init];
        [self.navigationController pushViewController:chooseController animated:YES];
    }

    NSLog(@"cell %@,%@ selected",@(indexPath.section),@(indexPath.row));
    
}

#pragma mark - LJBannerCellDelegate
- (void)bannerCellClick:(LJBannerCell *)forumTitleView andWithBtnTag:(NSInteger)tag {
    
}

#pragma mark - action
/**
 *  选择城市点击方法
 */
- (void)p_choiceCity {
    NSLog(@"%s",__func__);
}

/**
 *  点击搜索框事件
 */
- (void)p_startSearch {
    NSLog(@"%s",__func__);
}

- (void)initData {
    [self loadGroup0];
    [self loadGroup1];
    [self loadGroup2];
    [self loadGroup3];
}

- (void)loadGroup0 {
    // 创建组, 以及每组内的模型
    ShopGroup *group0 = [[ShopGroup alloc] init];
    group0.items = @[@"0"];
    [self.groups addObject:group0];
}

- (void)loadGroup1 {
    // 创建组, 以及每组内的模型
    ShopGroup *group1 = [[ShopGroup alloc] init];
    group1.items = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7"];
    [self.groups addObject:group1];
}

- (void)loadGroup2 {
    // 创建组, 以及每组内的模型
    ShopGroup *group4 = [[ShopGroup alloc] init];
    group4.items = @[@"0"];
    [self.groups addObject:group4];
}

- (void)loadGroup3 {
    // 创建组, 以及每组内的模型
    ShopGroup *group5 = [[ShopGroup alloc] init];
    group5.items = @[@"0",@"1",@"2",@"3",@"4"];
    [self.groups addObject:group5];
}


#pragma mark - Lazy
- (NSMutableArray *)groups {
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

- (UIView *)greenView {
    if (_greenView == nil) {
        _greenView = [[UIView alloc] init];
    }
    return _greenView;
}

@end
